package com.ruoyi.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.aliyun.oss.model.ObjectMetadata;


/**
 * 阿里云 OSS 工具类
 */
public class AliOSSUtils {

    private static final String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    private static final String accessKeyId = "LTAI5tJ43Rs16HdgHi6jFpEN";
    private static final String accessKeySecret = "8mE5R6SNrI6uJqMZL0kF0cGnZwfqJI";
    private static final String bucketName = "harper123-1";

    private static final String addr="oss-cn-hangzhou.aliyuncs.com";

    private static final ExecutorService executor = Executors.newFixedThreadPool(10); // 创建大小为10的固定线程池



    /**
     * 实现上传图片到OSS
     */
    public static String upload(MultipartFile multipartFile) throws IOException {
        // 获取上传的文件的输入流
        InputStream inputStream = multipartFile.getInputStream();

        // 避免文件覆盖
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")) + multipartFile.getOriginalFilename();

        //上传文件到 OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);

        //文件访问路径
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
        // 关闭ossClient
        ossClient.shutdown();
        return url;// 把上传到oss的路径返回
    }



    /**
     * 异步上传视频到OSS
     * @param videoFile 待上传的视频文件
     */
    public static String uploadVideoToOSS(MultipartFile videoFile) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String currentDate = dateFormat.format(new Date());
            String timestamp = String.valueOf(System.currentTimeMillis());
            String ossDirectory = currentDate + "/" + timestamp + "/";

            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(videoFile.getSize());

            String fileExtension = getFileExtension(videoFile.getOriginalFilename());
            switch (fileExtension.toLowerCase()) {
                case "mp4":
                    metadata.setContentType("video/mp4");
                    break;
                case "avi":
                    metadata.setContentType("video/x-msvideo");
                    break;
                case "mov":
                    metadata.setContentType("video/quicktime");
                    break;
                default:
                    metadata.setContentType("application/octet-stream");
                    break;
            }

            String ossFileName = ossDirectory + videoFile.getOriginalFilename();
            ossClient.putObject(bucketName, ossFileName, videoFile.getInputStream(), metadata);

            String videoUrl = "https://" + bucketName + "." + addr + "/" + ossFileName;
            return videoUrl;
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败: " + e.getMessage();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
    /**
     * 根据文件名获取对应的Content-Type
     * @param fileName 文件名
     * @return 对应的Content-Type
     */
    private static String getFileExtension(String fileName) {
        if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        } else {
            return "";
        }
    }

}
